<script setup lang="ts">
import { loginAPI } from '@/apis/user';
import { FORMDATA_KEY } from '@/constants';
import { useUserStore } from '@/stores/user';
import type { FormInstance } from 'element-plus';
import {onMounted, ref} from 'vue'
import { useRouter } from 'vue-router';
// 表单数据双向绑定
const formData = ref({
  username:'',
  password:'',
  remember:''
})
// 表单规则
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}
// 创建用户信息仓库
const userStore = useUserStore()
// 创建全局的路由实例对象，用于跳转
const router = useRouter()
// 通过ref获取form组件
const form = ref<FormInstance | undefined>()
// 全局效验
const doLogin = ()=> {
  form.value!.validate(async (valid) => {
    if (valid) {
      // TODO
      const {username,password} = formData.value
      // 登录
      const res = await loginAPI({username,password})
       // 记住我用户勾选了
      if (formData.value.remember) {
        // 用户名密码进行持久化存储
        localStorage.setItem(FORMDATA_KEY, JSON.stringify(formData.value))
      } else {
        // 移除
        localStorage.removeItem(FORMDATA_KEY)
      }
      // 将token存到pinia仓库中
      userStore.setToken(res.data.token)
      if (router.currentRoute.value.matched.length > 0) {
        router.back()
      } else {
        // 跳转到首页
        router.push('/')
      }
      
    }
  })
}

// 从本地取出数据赋值 
 onMounted(()=>{
  // 获取本地存储数据
  const jsonStr = localStorage.getItem(FORMDATA_KEY)
  // 如果不为空
  if(jsonStr !== null){
    // 将数据进行解析
    const userPassObj = JSON.parse(jsonStr)
    // 如果数据存在
    if (userPassObj) {
      // 将数据进行回到到表单
      formData.value = userPassObj
    }
  }
}) 
</script>

<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="formData" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" type="password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="formData.remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="doLogin()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>
